package com.neuronbit.businessflow.intellij.editor;


import com.intellij.designer.DesignerEditorPanelFacade;
import com.intellij.designer.LightToolWindowManager;
import com.intellij.designer.ToggleEditorModeAction;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.neuronbit.businessflow.intellij.editor.palette.impl.PaletteToolWindowManager;
import org.jetbrains.annotations.Nullable;

/**
 * The type Abstract tool window manager.
 *
 * @author Alexander Lobas
 */
public abstract class AbstractToolWindowManager extends LightToolWindowManager {
    /**
     * Instantiates a new Abstract tool window manager.
     *
     * @param project the project
     */
    protected AbstractToolWindowManager(Project project) {
        super(project);
    }

    @Nullable
    @Override
    protected DesignerEditorPanelFacade getDesigner(FileEditor editor) {
        if (editor instanceof BpmSwingFileEditor formEditor) {
            return formEditor.getEditor();
        }
        return null;
    }

    @Override
    protected ToggleEditorModeAction createToggleAction(ToolWindowAnchor anchor) {
        return new ToggleEditorModeAction(this, myProject, anchor) {
            @Override
            protected LightToolWindowManager getOppositeManager() {
                AbstractToolWindowManager designerManager = DesignerToolWindowManager.getInstance(myProject);
                AbstractToolWindowManager paletteManager = PaletteToolWindowManager.getInstance(myProject);
                return myManager == designerManager ? paletteManager : designerManager;
            }
        };
    }
}
